package com.rural.bus;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.rural.bus.config.MyPreferenceManager;
import com.rural.bus.config.PreferenceKey;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;
import com.rural.bus.utils.ProgressUtil;

public class ModifyPasswordActivity extends BaseActivity {
	private EditText et_new_psd, et_new_psd_twice, et_old_psd;
	private Button btn_submit;
	private TextView tvForgetPsw;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_password);

		initView();
		setListeners();

		initData();
	}

	protected void setListeners() {
		btn_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String password = et_new_psd.getText().toString().trim();
				String password2 = et_new_psd_twice.getText().toString().trim();
				String oldPsd = et_old_psd.getText().toString().trim();

				if (oldPsd.length() < 6 || password.length() < 6 || password2.length() < 6) {
					toast("密码必须大于6位");
					return;
				}
				if (!password.equals(password2)) {
					toast("两次输入的密码不一致");
					return;
				}

				modifyPassword(oldPsd, password);
			}
		});

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

	}

	@Override
	protected void initView() {
		btn_submit = (Button) findViewById(R.id.btn_submit);
		et_old_psd = (EditText) findViewById(R.id.et_old_psd);
		et_new_psd = (EditText) findViewById(R.id.et_new_psd);
		et_new_psd_twice = (EditText) findViewById(R.id.et_new_psd_twice);

		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("修改密码");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
	}

	@Override
	protected void initData() {

	}

	private void modifyPassword(String oldPsd, String password) {
//		String driver_id = MyPreferenceManager.instance().getSharedInfoByKey(PreferenceKey.USER_ID, "");
//		if (CommonTools.isEmpty(driver_id)) {
//			toast("数据异常，请退出后重新登陆");
//			return;
//		}
		MyHttpUtil http = new MyHttpUtil();
//		http.addBodyParm("driverId", driver_id);
		http.addBodyParm("password", oldPsd);
		http.addBodyParm("newpassword", password);
		ProgressUtil.showProgress(this, "");
		http.send(Url.update_password_url, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				try {
					if (object.get("status").equals("0")) {
						object = object.getJSONObject("result");
						toast("密码修改成功");
						ModifyPasswordActivity.this.finish();
					} else {
						toast("数据错误");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
			}
		});
	}

}
